$(document).ready(function(){
    $.ajaxSetup({
        cache: false,
        type: 'POST',
        dataType: 'json',
        error: function () {
            alert('Um erro de comunicação com o servidor ocorreu!');
            $('#ajax-ldr').fadeOut('fast');
            $('#wait').fadeOut('fast');
        }
    });
    $('#tabs').tabs();

    if ($('#aba').val()=='pesquisas'){
        $('#tabs').tabs('select',0);
    }else if ($('#aba').val()=='comerciais'){
        $('#tabs').tabs('select',1);
    }else if ($('#aba').val()=='tecnicas'){
            $('#tabs').tabs('select',2);
    }
    $("#antcom").click(function(){$('#aba').val('comerciais');var pag = $("#pagcom").val();if (pag>0){pag--;}$("#pagcom").val(pag);$('form').submit();return false;});
    $("#proxcom").click(function(event){$('#aba').val('comerciais');var pag = $("#pagcom").val();pag++;$("#pagcom").val(pag);$('form').submit();return false;});
    $("#antpesq").click(function(){$('#aba').val('pesquisas');var pag = $("#pagpesq").val();if (pag>0){pag--;}$("#pagpesq").val(pag);$('form').submit();return false;});
    $("#proxpesq").click(function(event){$('#aba').val('pesquisas');var pag = $("#pagpesq").val();pag++;$("#pagpesq").val(pag);$('form').submit();return false;});
    $("#anttec").click(function(){$('#aba').val('tecnicas');var pag = $("#pagtec").val();if (pag>0){pag--;}$("#pagtec").val(pag);$('form').submit();return false;});
    $("#proxtec").click(function(event){$('#aba').val('tecnicas');var pag = $("#pagtec").val();pag++;$("#pagtec").val(pag);$('form').submit();return false;});
    $("#filtro").change(function(){$("#pagina").val(0);});
    $("#chave").change(function(){$("#pagina").val(0);});

    $('.tips').tipsy({
        title:'obs',
        gravity: $.fn.tipsy.autoWE,
        html:true
    });
    $(".messages").fadeOut(6500);
//    tinyMCE.init({
//        mode : "textareas",
//        theme : "advanced"
    //    readonly : true
//    });
    $('textarea.tinymce').tinymce({
        // Location of TinyMCE script
        script_url : '/js/tiny_mce/tiny_mce.js',
        //readonly : false,
        // General options
        theme : "advanced",
        plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

        // Theme options
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect,|,forecolor,backcolor",
        theme_advanced_buttons2 : '',
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,

        // Example content CSS (should be your site CSS)
        content_css : "css/content.css",

        // Drop lists for link/image/media/template dialogs
        template_external_list_url : "lists/template_list.js",
        external_link_list_url : "lists/link_list.js",
        external_image_list_url : "lists/image_list.js",
        media_external_list_url : "lists/media_list.js",

        // Replace values for the template plugin
        template_replace_values : {
            username : "Some User",
            staffid : "991234"
        }
    });
    $('.relatorio').click(function(){
        var rel = $(this).attr('rel');
        var tipo = $(this).attr('tipo');
        //var ed = tinyMCE.get('relato');
        //ed.setContent('teste do anderson');
        $('#colaborador').html('<b>Colaborador:</b><br/>');
        $('#contato').html('<b>Contato:</b><br/>');
        if (tipo=='comercial'){$('#dialog').dialog({width:570,minWidth:350});}
        else{$('#dialog').dialog({width:850,minWidth:400});}
        $('#dialog').dialog('open');
        //alert(rel);
        if (tipo=='comercial'){
            $.ajax({
                url: "/roteiros/getrelatorio",
                processData: false,
                data: 'relatorio_id='+rel,
                beforeSend: function (){
                    var pos = $("#a_"+rel).offset();
                    var width = $("#a_"+rel).width();
    //                ed.setProgressState(0);
                    $("#ajax-ldr").css( {
                        "left": (pos.left+width+5) + "px",
                        "top":pos.top + "px"
                    } ).fadeIn('fast');
                },
                success: function(msg){
                    $('#ajax-ldr').fadeOut('fast');
                    $("#wait").fadeOut('fast');
                    $.each(msg['itensroteiro'],function(key,value){
                        //$('#relato').val(value.descricao);
                        //$('#aux').html(value.descricao);
                        //var ed = tinyMCE.get('relato');
                        //ed.setContent('teste do anderson');
                        $('#colaborador').html('<b>Colaborador:</b>   '        +value.nome);
                        $('#contato').html('<b>Contato Marcado:</b>   '        +value.contato.dsnome);
                        $('#contatoreal').html('<b>Contato Visitado:</b>   '   +value.contatoreal.dsnome);
                        $('#horario').html('<b>Horário Marcado:</b>   '        +value.horario);
                        $('#horarioreal').html('<b>Hoário Comparecido:</b>   '+value.horario_real);
                        $('#descricao').html(value.descricao);
                        //alert(value.descricao);
                    });
                }
            });
        }else{
           $.ajax({
                url: "/registros/getregistro",
                processData: false,
                data: 'id_registro='+rel,
                beforeSend: function (){
                    var pos = $("#a_"+rel).offset();
                    var width = $("#a_"+rel).width();
    //                ed.setProgressState(0);
                    $("#ajax-ldr").css( {
                        "left": (pos.left+width+5) + "px",
                        "top":pos.top + "px"
                    } ).fadeIn('fast');
                },
                success: function(msg){
                    $('#ajax-ldr').fadeOut('fast');
                    $("#wait").fadeOut('fast');
                    $('#colaborador').html('<b>Colaborador:</b>'+msg['relatorio'].cliente.nome);
                    $('#descricao').html('');
                    $.each(msg['atividades'],function(key,value){
                        $('#descricao').append($('<span>').attr('class','span-24')
                                                  .append($('<span>').attr('class','span-10')
                                                             .html(value.servico))
                                                  .append($('<span>').attr('class','span-5')
                                                             .append(value.sistema.descricao.substr(0,10)+' ('+value.modulo.descricao+')'))
                                                  .append($('<span>').attr('class','span-5 last')
                                                             .append(value.data+'-'+value.horini+ ' às '+value.horfin))
                                       );
                        //alert(value.descricao);
                    });
                }
            }); 
        }
        return false;
    });
    $("#dialog").dialog({
        dialogClass: 'alert',
        draggable: true,
        modal: true,
        title: 'Relatório de Visita Comercial',
        resizable: true,
        show: 'fadeIn',
        autoSize: true,
        autoOpen: false,
        minWidth: 350,
        width: 570,
        buttons: {
            'Ok':function(){
                //$('.error').hide();
                $('#dialog').dialog('close');
                return false;
            }
        }
    });
});
